﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Luna.Framework;
using System.Reflection;

namespace Luna
{
	public static class StringExtension
	{
		public static string format(this string template, params object[] args)
		{
			return string.Format(template, args);
		}

		public static bool isNotNullAndEmpty(this string s)
		{
			return !string.IsNullOrEmpty(s);
		}

		public static int toInt(this string template)
		{
			return int.Parse(template);
		}

		public static int toInt(this string value, int defaultValue) {
			int t;
			if (!int.TryParse(value, out t)) t = defaultValue;
			return t;
		}

		/// <summary>
		/// Return a Type object, if available, with a given name.
		/// </summary>
		/// <param name="s"></param>
		/// <returns></returns>
		public static Type getType(this string es) {
			int idx = es.IndexOf('/');
			if (idx >= 0)
				return Type.GetType(es.Substring(idx + 1) + ", " + es.Substring(0, idx), false, true);
			return Type.GetType(es, false, true);
		}

		public static string expandString(this string s) {
			return Framework.Framework.expandString(s);
		}

		public static IList<string> splitEx(this string s, params char[] separators) {
			IList<string> result = new List<string>();
			foreach (string e in s.Split(separators))
				if (e.Trim().isNotNullAndEmpty()) result.Add(e.Trim());
			return result;
		}
	}
}
